package com.cyl.wms.scheduler;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class PrinterTask {
    //@Scheduled(fixedDelay = 1000000L,initialDelay = 5000)
    public void print(){

        /*try {
            // 创建一个连接到Zebra打印机的套接字
            Socket socket = new Socket("192.168.10.233", 9100);

            // 打开输出流
            OutputStream outputStream = socket.getOutputStream();

            // 发送打印命令
            String command = "^XA^FO50,50^ADN,36,20^FDHello World^FS^XZ";
            outputStream.write(command.getBytes());

            // 关闭输出流和套接字
            outputStream.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        try {
            // 创建一个连接到Zebra打印机的套接字
            Socket socket = new Socket("192.168.117.233", 9100);

            // 打开输出流
            OutputStream outputStream = socket.getOutputStream();

            Map dto = new HashMap();
            /*^XA
            ^CW1,E:SIMSUN.TTF
            ^CI28
            ^FO150,60^BQN,2,5^FDQA,{{name}}|{{lot}}|{{type}}|{{inspectionDate}}|{{storageDate}}|{{retestDate}}|{{model}}|{{nco}}|{{oh}}|{{p}}|^FS
            ^FO410,60^A1N,20,20^FD名称代码: {{name}}^FS
            ^FO410,100^A1N,20,20^FD批次编号: {{lot}}^FS
            ^FO410,140^A1N,20,20^FD物料类型: {{type}}^FS
            ^FO410,180^A1N,20,20^FD检验日期: {{inspectionDate}}^FS
            ^FO410,220^A1N,20,20^FD存放日期: {{storageDate}}^FS
            ^FO410,260^A1N,20,20^FD复验日期: {{retestDate}}^FS
            ^FO410,300^A1N,20,20^FD限用型号: {{model}}^FS
            ^FO410,340^A1N,20,20^FDNCO: {{nco}}^FS
            ^FO410,380^A1N,20,20^FDOH : {{oh}}^FS
            ^FO410,420^A1N,20,20^FDP : {{p}}^FS
            ^PR3^PQ1^XZ*/
            dto.put("name","黎庆剑");
            dto.put("lot","黎庆剑");
            dto.put("type","黎庆剑");
            dto.put("inspectionDate","黎庆剑");
            dto.put("storageDate","黎庆剑");
            dto.put("retestDate","黎庆剑");
            dto.put("model","黎庆剑");
            dto.put("nco","黎庆剑");
            dto.put("oh","黎庆剑");
            dto.put("p","黎庆剑");

            // 发送打印命令
            /*List<String> commands = getTemplate(dto);
            for (int i = 0; i < commands.size(); i++) {
                String command = commands.get(i);
                outputStream.write(command.getBytes());
            }*/
            String ss = "\n" +
                    "^XA\n" +
                    "^CI0\n" +
                    "^FO100,50^A0N,30,30^FDUTF8 CI0 (M,B) Mode^FS\n" +
                    "^FO100,100^BQ,3,3\n" +
                    "^FDQM,B0600中文-斑马帮帮帮^FS\n" +
                    "^XZ";
            outputStream.write(ss.getBytes());

            // 关闭输出流和套接字
            outputStream.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * description: 获取打印模板
     */
    public List<String> getTemplate(Map dto) {
        List<String> templateList = new ArrayList<>();
        String nextTemplate = readTemplate(PrintConstant.PRINT_TEMPLATE);
        //Map<String, Object> params = ObjectToMapUtils.objectToMap(dto);
        nextTemplate = parseTemplate(dto, nextTemplate)+"\n";
        templateList.add(nextTemplate);
        return templateList;
    }

    private static String parseTemplate(Map<String, Object> params, String templateName) {
        if (params != null) {
            for (String key : params.keySet()) {
                if (!"list".equals(key)) {
                    templateName = templateName.replaceAll("\\{\\{" + key + "\\}\\}", params.get(key) != null ? params.get(key).toString() : "");
                }
            }
        }
        return templateName;
    }

    /**
     * description: 读取zpl的内容
     */
    private static String readTemplate(String templateName){
        String printTpl  = new FileReader(File.separator + templateName).getContent();
        return printTpl;
    }
}
